<?php

if (!defined('IN_WONDERSHARE'))
{
	die('Hacking attempt');
}
require('./classes/module/module_product.php');
class ControlProduct{
	function __construct(){
	$this -> ModuleProduct = new ModuleProduct();
	}

	/**************************************
	* 得到won_product中的主键和产品品牌
	* @param  
	* @return  int
	**************************************/
	function getWonProductInfo($product_id)
	{
		$sql = "SELECT won_p.id, p.product_brand 
								FROM product p 
								LEFT JOIN won_product won_p ON won_p.won_product_id = p.product_id 
								WHERE p.product_id = {$product_id} AND won_p.won_product_delete = 0 	";
		return  $GLOBALS['db']->getOneRs($sql);
	}
	/**************************************
	* 查询所有未删除的公共产品信息的分页数据
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function getProductList($start,$pagesize)
	{
		$sql="select * from product where `product_delete`=0 order by `product_id` LIMIT $start,$pagesize";
		$result = $this -> ModuleProduct->AllProduct($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 查询所有未删除的公共产品信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function getProductListOne($product_id = 0,$product_platform = '',$type = '' )
	{
		if($type =='product_edit')
		{
			$where_str = "";
			$where_str = $product_id != 0?' AND p.product_id !='.$product_id:"";
			if($product_platform == 'Win')
			{
				$where_str .= " AND p.product_os ='Mac' ";
			}elseif ($product_platform == 'Mac')
			{
				$where_str .= " AND p.product_os ='Win' ";
			}
			//$sql="SELECT  * FROM  product WHERE  `product_delete`=0 $where_str order by `product_id`";
			$sql="select p.*,l.price from product as p left join license as l on p.product_id=l.product_id 
			where p.`product_delete`=0 $where_str and l.`website_display`=1 order by p.`product_id`";
		}else
		{
			$sql="select p.*,l.price from product as p left join license as l on p.product_id=l.product_id 
			where p.`product_delete`=0 and l.`website_display`=1 order by p.`product_id`";
		}
		//die($sql);
		$result = $this -> ModuleProduct->AllProduct($sql);
		return $result ? $result : NULL;
	
	}

	/**************************************
	* 查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function getProductListPage($page)
	{
		$sql="SELECT COUNT(*) AS total FROM product WHERE `product_delete`=0";
		$page_info = get_page_info($sql,15,$page);
		$page_url = get_page_url($page,$page_info['page_num']);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}

	/**************************************
	* 根据产品ID查询对应公共产品的所有信息
	* @access  public
	* @param   int     $product_id
	*
	* @return  array
	**************************************/
	function ProductDetail($product_id)
	{
		$sql="select * from product where `product_id`=".$product_id." and `product_delete`=0 ";
		$result = $this -> ModuleProduct->ProductDetail($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 按照查询条件来查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function SearchProductListPage($page,$product_brand,$search_type,$search_key)
	{
		$sql="SELECT COUNT(*) AS total FROM product WHERE `product_delete`=0";
		if(!empty($product_brand))
		{
			$sql.=" AND `product_brand` = '".$product_brand."'";
		}
		if (!empty($search_key))
		{
			if($search_type==1){
				$sql.=" AND `product_cbsid` = '{$search_key}'";
			}else{
				$sql.=" AND `product_name` like '%".$search_key."%'";
			}
		}
	
		$page_info = get_page_info($sql,15,$page);
		$page_url = get_page_url($page,$page_info['page_num'],'&product_brand='.$product_brand.'&search_type='.$search_type.'&search_key='.$search_key);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}

	/**************************************
	* 按照查询条件来查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function SearchProductList($start,$pagesize,$product_brand,$search_type,$search_key)
	{
		$sql="select * from product where `product_delete`=0";
		if(!empty($product_brand))
		{
			$sql.=" AND `product_brand` = '".$product_brand."'";
		}
		if (!empty($search_key))
		{
			if($search_type==1)
			{
				$sql.=" AND `product_cbsid` = '{$search_key}'";
			}
			else 
			{
				$sql.=" AND `product_name` like '%".$search_key."%'";
			}
		}
		$sql.=" order by `product_id` LIMIT $start,$pagesize";
		$result = $this -> ModuleProduct->AllProduct($sql);
		return $result ? $result : NULL;
	}
	/**************************************
	* 根据产品ID到CBS中查询对应公共产品的相关信息信息
	* @access  public
	* @param   int     $product_id
	*
	* @return  array
	**************************************/
	function GetCBSdata($product_id)
	{
		$product_url = "http://cbsapi.wondershare.cn/interface.php?m=cms_product&type=product_content&pids=" . trim($product_id); 
		$product_info = json_decode(file_get_contents($product_url),true);
		return $product_info[$product_id];
	}
//	function GetCBSdata($product_id)
//	{
//		  $product_url = "http://cbsapi.wondershare.cn/interface.php?m=cms_product&type=product_content&pids=" . trim($product_id); 
//		  $curlHandle = curl_init();
//		  curl_setopt( $curlHandle , CURLOPT_URL, $product_url );
//		  curl_setopt( $curlHandle , CURLOPT_RETURNTRANSFER, 1 );
//		  curl_setopt( $curlHandle , CURLOPT_TIMEOUT, 2 );
//		  $result = curl_exec( $curlHandle );
//		  curl_close( $curlHandle );
//		  $product_info = json_decode($result,true);
//		  return $product_info[$product_id];
//	}
	/**************************************
	* 根据产品ID到公共产品表的相关信息信息
	* @access  public
	* @param   int     $product_id
	*
	* @return  array
	**************************************/
	function DecideCBSdata($product_cbsid)
	{
		$sql="select * from product where `product_cbsid`=".$product_cbsid." and `product_delete`=0 ";
		$result = $this -> ModuleProduct->DecideCBSdata($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 获取在售的版本
	* @param array $product_info   产品信息
	* @return array
	**************************************/
	function get_sale_version($product_id){
		$product_id=intval($product_id);
		$sql='SELECT * FROM VERSION WHERE `product_id`='.$product_id.' AND `is_sale`=1 ORDER BY version_name DESC LIMIT 0,1';
		$versions = $this->ModuleProduct->AllProduct($sql);
		return $versions;
	}


	/**************************************
	* 获取价格
	* @param array $product_id   当前产品数组
	* @return array
	**************************************/
	function get_price($product_id)
	{
		$product_id=intval($product_id);
		$sql="SELECT * FROM license WHERE `product_id`=".$product_id;
		$prices = $this->ModuleProduct->AllProduct($sql);
		$prices = is_array($prices) ? $prices : array();
		$price = array();
		foreach($prices as $item){
		
			if($item['license_id'] == 11){
				if($item['sub_license_id'] == 0 )
				{
					if($item['website_display'])
					{
						$price['curr'] = $item['price'];
					}
					else 
					{
						$price['old'] = $item['price'];
						$price['unique'] = true;
					}
				}
				else
				{
					if($item['website_display'])
					{
						$price['curr'] = $item['price'];
					}
				}
			}
		}
		if(empty($price['curr'])){
			$price['curr'] = $price['old'];
			$price['unique'] = true;
		}
		return $price;
	}

	/**************************************
	* 获取What's new输出
	* @param array $product_list 产品列表
	* @return <type> 
	**************************************/
	function getWhatNew($productInfo){
		$version_list = $productInfo['version'];
		
		$html = '<div id="content2">';
		$html .= '<h2>What\'s new ...</h2>';
		if (is_array($version_list))
		{
			foreach($version_list as $key => $val){
				$title = $val['whatisnew_title'];
				$content = $val['whatisnew_content'];
				$content = str_replace("\n","<br>",$content);
				$html .= "<<<HTML
				<div class='stretch2'>
				<ul>
				<li class='tit'>$title</li>
				<li>$content</li>
				</ul>
				</div>        
				HTML";
			}
		}
		if(count($version_list) > 4){
			$html .= '<a class="panel_button">more</a>';
		}
		$html .= '</div>';
		
		return $version_list;
	}

	/**************************************
	* 处理产品信息的每个字段
	* @access  public
	* @param   array   $product_data  产品信息组
	*
	* @return  array
	**************************************/
	function ManageProductData($product_data)
	{
		if(empty($product_data))
		{
			return false;
		}
		else
		{
			$product['product_cbsid']=intval($product_data['product_cbsid']);
			$product['product_id']=intval($product_data['product_id']);
			$product['product_sn']=intval($product_data['product_sn']);
			$product['product_name']=trim($product_data['product_name']);
			$product['product_version']=trim($product_data['product_version']);
			$product['product_brand']=trim($product_data['product_brand']);
			$product['product_price']=floatval($product_data['product_price']);
			$product['product_price_des']=trim($product_data['product_price_des']);
			$product['product_os']=trim($product_data['product_os']);
			$product['product_isfree']=trim($product_data['product_isfree']);
			$product['product_type']=trim($product_data['product_type']);
			$product['product_rel']=trim($product_data['product_rel']);
			$product['product_support_intro']=trim($product_data['product_support_intro']);
			$product['product_support_content']=trim($product_data['product_support_content']);
			$product['product_icon_min']=trim($product_data['product_icon_min']);
			$product['product_icon_max']=trim($product_data['product_icon_max']);
			$product['product_box_min']=trim($product_data['product_box_min']);
			$product['product_box_middle']=trim($product_data['product_box_middle']);
			$product['product_box_big']=trim($product_data['product_box_big']);
			$product['product_video_guide']=trim($product_data['product_video_guide']);
			$product['product_video_promo']=trim($product_data['product_video_promo']);
			$product['product_pic_awards']=trim($product_data['product_pic_awards']);
			$product['product_ad']=trim($product_data['product_ad']);
			$product['product_intro']=trim($product_data['product_intro']);
			$product['product_content']=trim($product_data['product_content']);
			$product['product_key_feature']=trim($product_data['product_key_feature']);
			$product['product_recomm']=trim($product_data['product_recomm']);
			$product['product_head_pic']=trim($product_data['product_head_pic']);
			$product['product_pages']=trim($product_data['product_pages']);
			$product['product_add_user']=trim($product_data['product_add_user']);
			$product['product_add_time']=trim($product_data['product_add_time']);
			$product['product_updata_time']=trim($product_data['product_updata_time']);
			$product['product_state']=trim($product_data['product_state']);
			$product['product_is_recommend']=trim($product_data['product_is_recommend']);
			
			return $product;
		}
	}

	/**************************************
	* 修改产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	function EditProductData($product)
	{
		$sql="UPDATE product SET `product_name`='".$product["product_name"]."',`product_version`='".$product["product_version"]."',
		`product_price`='".$product["product_price"]."',`product_price_des`='".$product["product_price_des"]."',
		`product_brand`='".$product["product_brand"]."',`product_os`='".$product["product_os"]."',
		`product_isfree`='".$product["product_isfree"]."',`product_type`='".$product["product_type"]."',
		`product_rel`='".$product["product_rel"]."',`product_support_intro`='".$product["product_support_intro"]."',
		`product_support_content`='".$product["product_support_content"]."',`product_icon_min`='".$product["product_icon_min"]."',
		`product_icon_max`='".$product["product_icon_max"]."',`product_box_min`='".$product["product_box_min"]."',
		`product_box_middle`='".$product["product_box_middle"]."',`product_box_big`='".$product["product_box_big"]."',
		`product_video_guide`='".$product["product_video_guide"]."',`product_video_promo`='".$product["product_video_promo"]."',
		`product_pic_awards`='".$product["product_pic_awards"]."',`product_recomm`='".$product["product_recomm"]."',
		`product_ad`='".$product["product_ad"]."',`product_intro`='".$product["product_intro"]."',
		`product_content`='".$product["product_content"]."',`product_key_feature`='".$product["product_key_feature"]."',
		`product_head_pic`='".$product["product_head_pic"]."',`product_pages`='".$product["product_pages"]."',
		`product_update_time`='".$product["product_update_time"]."',`product_state`='".$product["product_state"]."',
		`product_is_recommend`='".$product["product_is_recommend"]."',`product_sn`='".$product["product_sn"]."',
		`product_cbsid`='".$product["product_cbsid"]."' where `product_id`=".$product['product_id'];
		
		$result = $this -> ModuleProduct->EditProductData($sql);
		return $result;
	}

	/**************************************
	* 保存产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	function AddProductData($product)
	{
		$sql="INSERT INTO product (`product_name`,`product_version`,`product_price`,`product_price_des`,
		`product_brand`,`product_os`,`product_isfree`,`product_type`,
		`product_rel`,`product_support_intro`,`product_support_content`,`product_icon_min`,
		`product_icon_max`,`product_box_min`,`product_box_middle`,`product_box_big`,
		`product_video_guide`,`product_video_promo`,`product_pic_awards`,`product_recomm`,
		`product_ad`,`product_intro`,`product_content`,`product_key_feature`,
		`product_head_pic`,`product_pages`,`product_add_user`,`product_add_time`,
		`product_state`,`product_is_recommend`,`product_sn`,`product_cbsid`) VALUES(
		'".$product["product_name"]."','".$product["product_version"]."','".$product["product_price"]."','".$product["product_price_des"]."',
		'".$product["product_brand"]."','".$product["product_os"]."','".$product["product_isfree"]."','".$product["product_type"]."',
		'".$product["product_rel"]."','".$product["product_support_intro"]."','".$product["product_support_content"]."','".$product["product_icon_min"]."',
		'".$product["product_icon_max"]."','".$product["product_box_min"]."','".$product["product_box_middle"]."','".$product["product_box_big"]."',
		'".$product["product_video_guide"]."','".$product["product_video_promo"]."','".$product["product_pic_awards"]."','".$product["product_recomm"]."',
		'".$product["product_ad"]."','".$product["product_intro"]."','".$product["product_content"]."','".$product["product_key_feature"]."',
		'".$product["product_head_pic"]."','".$product["product_pages"]."','".$product["product_add_user"]."','".$product["product_add_time"]."',
		'".$product["product_state"]."','".$product["product_is_recommend"]."','".$product["product_sn"]."','".$product["product_cbsid"]."')";
		
		$result = $this -> ModuleProduct->AddProductdata($sql);
		return $result;
	}

	/**************************************
	* 删除产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	
	function DelProduct($product_id)
	{
		$sql="UPDATE product SET `product_delete`=1 where `product_id`=".$product_id;
		$result = $this -> ModuleProduct->DelProduct($sql);
		
		$this->clear_ws_download_cache();
		return $result;
	}

	/**************************************
	* 发布产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	
	function StateProduct($product_id)
	{
		$sql="UPDATE product SET `product_state`=1 where `product_id`=".$product_id;
		$result = $this -> ModuleProduct->StateProduct($sql);
		return $result;
	}

	/**************************************
	* 取消发布产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	
	function CancelStateProduct($product_id)
	{
		$sql="UPDATE product SET `product_state`=0 where `product_id`=".$product_id;
		$result = $this -> ModuleProduct->StateProduct($sql);
		return $result;
	}

	/**************************************
	* 推荐产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	
	function RecommendProduct($product_id)
	{
		$sql="UPDATE product SET `product_is_recommend`=1 where `product_id`=".$product_id;
		$result = $this -> ModuleProduct->RecommendProduct($sql);
		return $result;
	}

	/**************************************
	* 取消推荐产品信息
	* @access  public
	* @param   array   $product  产品信息组
	*
	* @return  string
	**************************************/
	
	function CancelRecommendProduct($product_id)
	{
		$sql="UPDATE product SET `product_is_recommend`=0 where `product_id`=".$product_id;
		$result = $this -> ModuleProduct->RecommendProduct($sql);
		return $result;
	}

	/**************************************
	* 保存产品的价格信息
	* @access  public
	* @param   int   $product_cbsid  产品在CBS中的ID
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	function AddProductPrice($product_cbsid,$product_id)
	{
		$product_data=$this->GetCBSdata($product_cbsid);
		$product_price=$product_data['price'];
		$product_sn=$product_data['product_num'];
		$operate_time=$product_data['publish_date'];
		$operater=$_SESSION['AName'];
		if($product_price)
		{
			foreach ($product_price as $price)
			{
				$sql="INSERT INTO license (`license_id`,`sub_license_id`,`license`,`website_display`,
				    `swreg_url`,`regnow_url`,`cart_url`,`price`,`product_sn`,`product_cbs_id`,
				    `product_id`,`operate_time`,`operater`) VALUES(
				    '".$price["license_id"]."','".$price["sub_license_id"]."','".$price["license"]."','0',
				    '".$price["swreg_url"]."','".$price["regnow_url"]."','".$price["cart_url"]."',
				    '".$price["price"]."','".$product_sn."','".$product_cbsid."','".$product_id."','".$operate_time."','".$operater."')";
				$result = $this -> ModuleProduct->AddProductPrice($sql);
			}
		}
		return $result;
	}


	/**************************************
	* 同步CBS产品的价格信息
	* @access  public
	* @param   int   $product_cbsid  产品在CBS中的ID
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	
	function sync_price_cbs($product_price,$product_id,$product_cbsid,$product_sn,$product_name)
	{
		$product_cbsid=intval($product_cbsid);
		$product_id=intval($product_id);
		$operate_time=date("Y-m-d H:i:s");
		$operater=$_SESSION['AName'];
		$product_price=is_array($product_price)?$product_price:array();
		foreach ($product_price as $price)
		{
	        //判断是否是日语产品
	        if(strstr(strtolower($product_name),'japanese')){
	        	$price["price"] = $price['JPY'];
	        }
	        
			$license_id=$price['license_id'];
			$sub_license_id=$price['sub_license_id'];
			$sql='SELECT COUNT(*) FROM license WHERE `license_id`='.$license_id.' AND `sub_license_id`='.$sub_license_id.' AND `product_cbs_id`='.$product_cbsid.' AND `product_id`='.$product_id;
			$ver_count=$this->ModuleProduct->sync_version_cbs($sql);
			if($ver_count)
			{
			$sqla="UPDATE license SET `license_id`='".$price['license_id']."',`sub_license_id`='".$price['sub_license_id']."',
			`license`='".$price['license']."',`swreg_url`='".$price['swreg_url']."',`shareit_url`='".$price['shareit_url']."',
			`regnow_url`='".$price['regnow_url']."',`cart_url`='".$price['cart_url']."',`price`='".$price['price']."',
			`product_sn`='".$product_sn."',`product_cbs_id`='".$product_cbsid."',`product_id`='".$product_id."',
			`operate_time`='".$operate_time."',`operater`='".$operater."'
			WHERE `license_id`=".$license_id." AND `sub_license_id`=".$sub_license_id." AND `product_cbs_id`=".$product_cbsid." AND `product_id`=".$product_id;
			$result=$this->ModuleProduct->UpdataPriceProduct($sqla);
			}
			else 
			{
			$sqlb="INSERT INTO license (`license_id`,`sub_license_id`,`license`,`website_display`,
			    `swreg_url`,`regnow_url`,`shareit_url`,`cart_url`,`price`,`product_sn`,`product_cbs_id`,
			    `product_id`,`operate_time`,`operater`) VALUES(
			    '".$price["license_id"]."','".$price["sub_license_id"]."','".$price["license"]."','0',
			    '".$price["swreg_url"]."','".$price["regnow_url"]."','".$price["shareit_url"]."','".$price["cart_url"]."',
			    '".$price["price"]."','".$product_sn."','".$product_cbsid."','".$product_id."','".$operate_time."','".$operater."')";
			$result = $this -> ModuleProduct->AddProductPrice($sqlb);
			//					$sqlc="UPDATE license SET `website_display`='1' WHERE `license_id`=11 AND `sub_license_id`=0 AND `product_cbs_id`=".$product_cbsid." AND `product_id`=".$product_id;
			//					$results=$this->ModuleProduct->UpdataPriceProduct($sqlc);
			}
		}
		$sql = 'UPDATE license SET website_display = 1  WHERE  license_id = 11 AND sub_license_id = 0 AND  product_id= '.$product_id.'
		AND NOT EXISTS (SELECT  * FROM (SELECT 1 FROM license WHERE  license_id =11 AND  website_display =1 AND product_id = '.$product_id.') a )';
		$GLOBALS['db']->query($sql);
		$sqld="select `license_id`,`sub_license_id` from license where `product_cbs_id`=".$product_cbsid." AND `product_id`=".$product_id;
		$price_res=$this->ModuleProduct->AllProduct($sqld);
		foreach ($price_res as $res)
		{
			$i=1;
			foreach ($product_price as $pro)
			{
				if($res['license_id']==$pro['license_id']&&$res['sub_license_id']==$pro['sub_license_id'])
				{
				$i=0;
				}
			}
			if($i)
			{
				$sqle="DELETE FROM license  WHERE `license_id`=".$res['license_id']." AND `sub_license_id`=".$res['sub_license_id']." AND `product_cbs_id`=".$product_cbsid." AND `product_id`=".$product_id;
				$results=$this->ModuleProduct->UpdataPriceProduct($sqle);
			}
		}
		return $result;
	}

	/**************************************
	* 保存产品的版本信息信息
	* @access  public
	* @param   int   $product_cbsid  产品在CBS中的ID
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	
	function AddProductVersion($product_cbsid,$product_id)
	{
		$product_data=$this->GetCBSdata($product_cbsid);
		$product_version=$product_data['version'];
		$product_sn=$product_data['product_num'];
		//$publish_date=$product_data['publish_date'];
		$operater=$_SESSION['AName'];
		if($product_version)
		{
			foreach ($product_version as $version)
			{
				$sql="INSERT INTO version (`version_id`,`version_name`,`is_sale`,`publish_date`,
				    `download_url`,`whatisnew_title`,`whatisnew_content`,`product_sn`,`product_cbs_id`,`product_id`,`operater`) VALUES(
				    '".$version["version_id"]."','".$version["version_name"]."','".$version["is_sale"]."','".$version["publish_date"]."',
				    '".$version["download_url"]."','".addslashes($version["whatisnew_title"])."','".addslashes($version["whatisnew_content"])."',
				    '".$product_sn."','".$product_cbsid."','".$product_id."','".$operater."')";
				$result = $this -> ModuleProduct->AddProductPrice($sql);
			}
		}
		return $result;
	}

	/**************************************
	* 根据产品的ID查询产品的价格信息的分页信息
	* @access  public
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	function ManagePrice($product_id,$page)
	{
		$product_id=intval($product_id);
		$page=intval($page);
		$sql="select count(*) as total from license where `product_id`=".$product_id;
		
		$page_info = get_page_info($sql,100,$page);
		$page_url = get_page_url($page,$page_info['page_num']);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}

	/**************************************
	* 根据产品的ID查询产品的版本信息的分页信息
	* @access  public
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	function ManageVersion($product_id,$page)
	{
		$product_id=intval($product_id);
		$page=intval($page);
		$sql="select count(*) as total from version where `product_id`=".$product_id;
		
		$page_info = get_page_info($sql,15,$page);
		$page_url = get_page_url($page,$page_info['page_num']);
		return  array('page_info'=>$page_info,'page_url'=>$page_url);
	}

	/**************************************
	* 根据产品的ID查询产品的价格信息的分页数据
	* @access  public
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	function ManagePricePage($start,$pagesize,$product_id)
	{
		$start=intval($start);
		$pagesize=intval($pagesize);
		$product_id=intval($product_id);
		$sql="select * from license where `product_id`=".$product_id." order by `license_id` LIMIT $start,$pagesize";
		
		$result = $this -> ModuleProduct->ManagePrice($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 根据产品的ID查询产品的版本信息的分页数据
	* @access  public
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	function ManageVersionPage($start,$pagesize,$product_id)
	{
		$start=intval($start);
		$pagesize=intval($pagesize);
		$product_id=intval($product_id);
		$sql="select * from version where `product_id`=".$product_id." order by `version_id` desc LIMIT $start,$pagesize";
		$result = $this -> ModuleProduct->ManagePrice($sql);
		return $result ? $result : NULL;
	}

	/**************************************
	* 根据价格表的ID查询产品的价格信息
	* @access  public
	* @param   int   $product_id     产品在公共产品表中的ID
	*
	* @return  string
	**************************************/
	function ProductPriceDetail($id)
	{
		$id=intval($id);
		$sql="select * from license where `id`=".$id;
		$result = $this -> ModuleProduct->ProductPriceDetail($sql);
		return $result;
	}

	function UpdataPriceProduct($price_id,$price_value)
	{
		$sql="UPDATE license SET `website_display`=".$price_value." where `id`=".$price_id;
		$result = $this -> ModuleProduct->UpdataPriceProduct($sql);
		return $result;
	}

	function UpdataSonPriceProduct($price_id,$price_value,$license_id,$product_id)
	{
		$sql="UPDATE license SET `website_display`=".$price_value." where `id`=".$price_id;
		$result = $this -> ModuleProduct->UpdataPriceProduct($sql);
		$sqla="UPDATE license SET `website_display`=0 WHERE `id`!=".$price_id." AND `license_id`=".$license_id." AND `product_id`=".$product_id;
		$result = $this -> ModuleProduct->UpdataPriceProduct($sqla);
		return $result;
	}

	/**************************************
	* 按照查询条件来查询所有未删除的公共产品信息的分页数据信息
	* @access  public
	* @param  
	*
	* @return  array
	**************************************/
	function SearchProductListInDetail($product_is_recommend,$product_state,$search_type,$search_key,
	$product_plat='',$product_id = 0,$product_brand ='')
	{
		
		$sql="select * from product where `product_delete`=0";
		if(!empty($product_is_recommend))
		{
			$product_is_recommend = $product_is_recommend == 2?0:$product_is_recommend ;
			$sql.=" AND `product_is_recommend` = ".$product_is_recommend;
		}
		if(!empty($product_state))
		{
			$product_state = $product_state == 2?0:$product_state ;
			$sql.=" AND `product_state` = ".$product_state;
		}
		if (!empty($search_key))
		{
			
			if($search_type == 0)
			{
				$sql.=" AND `product_sn` like '%".$search_key."%'";
			}
			else 
			{
				$sql.=" AND `product_name` like '%".$search_key."%'";
			}
		}
		if($product_plat == 'Win')
		{
			$sql .= " AND product_os ='Mac' ";
		}elseif ($product_plat == 'Mac')
		{
			$sql .= " AND product_os ='Win' ";
		}
		if(!empty($product_id))
		{
			$sql .= ' AND product_id != '.$product_id; 
		}
		if(!empty($product_brand))
		{
			$sql .= ' AND product_brand = "'.$product_brand.'"'; 
		}
		$sql.=" order by `product_id`";

		$result = $this -> ModuleProduct->AllProduct($sql);
		return $result ? $result : NULL;
	}
	
	/**************************************
	* 同步产品CBS版本信息
	* @access  public
	* @param   int     $_REQUEST['product_id']
	*
	* @return  array
	**************************************/
	function sync_version_cbs($product_version,$product_id,$product_cbsid,$product_sn)
	{
		$product_cbsid=intval($product_cbsid);
		$product_id=intval($product_id);
		//$publish_date=date("Y-m-d H:i:s");
		$operater=$_SESSION['AName'];
		$product_version=is_array($product_version)?$product_version:array();
		foreach ($product_version as $version)
		{
			$version_id=$version['version_id'];
			$sql='SELECT COUNT(*) FROM version WHERE `version_id`='.$version_id.' AND `product_cbs_id`='.$product_cbsid.' AND `product_id`='.$product_id;
			$ver_count=$this->ModuleProduct->sync_version_cbs($sql);
			if($ver_count)
			{
				$sqla="UPDATE version SET `version_id`='".$version['version_id']."',`version_name`='".$version['version_name']."',
				`is_sale`='".$version['is_sale']."',`publish_date`='".$version['publish_date']."',`download_url`='".$version['download_url']."',
				`whatisnew_title`='".addslashes($version['whatisnew_title'])."',`whatisnew_content`='".addslashes($version['whatisnew_content'])."',
				`product_sn`='".$product_sn."',`product_cbs_id`='".$product_cbsid."',`operater`='".$operater."',`product_id`='".$product_id."' 
				where `version_id`=".$version_id." AND `product_cbs_id`=".$product_cbsid." AND `product_id`=".$product_id;
				$result=$this->ModuleProduct->UpdataPriceProduct($sqla);
			}
			else 
			{
				$sqlb="INSERT INTO version (`version_id`,`version_name`,`is_sale`,`publish_date`,
				`download_url`,`whatisnew_title`,`whatisnew_content`,`product_sn`,`product_cbs_id`,`product_id`,`operater`) VALUES(
				'".$version["version_id"]."','".$version["version_name"]."','".$version["is_sale"]."','".$version['publish_date']."',
				'".$version["download_url"]."','".addslashes($version["whatisnew_title"])."','".addslashes($version["whatisnew_content"])."',
				'".$product_sn."','".$product_cbsid."','".$product_id."','".$operater."')";
				$result = $this -> ModuleProduct->AddProductPrice($sqlb);
			}
		}
		return $result;
	}

	function Version_new($version_id)
	{
		$version_id=intval($version_id);
		$sql="SELECT `version_name`,`download_url`,`whatisnew_content` FROM version WHERE `v_id`=".$version_id; 
		$result=$this->ModuleProduct->ProductPriceDetail($sql);
		return $result;
	}

	function UpdataVersion($version_id,$sale_type)
	{
		$publish_date=date("Y-m-d H:i:s");
		$operater=$_SESSION['AName'];
		$sql="UPDATE version SET `is_sale`='".$sale_type."',`publish_date`='".$publish_date."',`operater`='".$operater."' WHERE `v_id`=".$version_id;
		$result=$this->ModuleProduct->UpdataPriceProduct($sql);
	}
	/**************************************
	* 得到公共表中所有品牌
	* @return  array
	**************************************/
	function product_brand()
	{
		$sql="SELECT DISTINCT  `product_brand` FROM product";
		$result=$this->ModuleProduct->AllProduct($sql);
		return $result;
	}

	//note 删除ws的download页面在CMS的缓存
	function clear_ws_download_cache()
	{
		$file = glob('../image2/cache/ws_download-*.php');
		foreach( $file as $v ){
			@unlink( $v );
		}
	}
	
	 /**
    * 修改与发布模板操作
    * @param   int      $product_id  公共产品id
    * @param   string    $location  链接
    * @param   int      $is_ajax  是否是ajax请求
    * @return  string
    */
    function updateTemplate($product_id = 0,  $location = '', $is_ajax =0 ) {
        
        $rows = $this->getWonProductInfo($product_id);
      
        
        //定义更新分类模板
        $update_arr = array('id' => $rows['id'],
                            'w_type' => (int) brandToWebtype($rows['product_brand']),
                            'operation_type' => 'product',
                            'location' => $location,
                            'is_ajax' => $is_ajax,
                            
        );
      
        if ($is_ajax == 1) {
            file_get_contents("http://cms.wondershare.com/wondershare/template.php?action=updateTemplate&type=update&".
                                http_build_query($update_arr));
        } else {
            
            echo '<script type="text/javascript">
                window.location.href="/wondershare/template.php?action=updateTemplate&type=update&'.http_build_query($update_arr).'";
                </script>';
        }
    }
      

}
?>